package org.platform.lwc.system.service.impl;


import org.platform.lwc.system.mongodb.document.MemberReadHistory;
import org.platform.lwc.system.mongodb.repository.MemberReadHistoryRepository;
import org.platform.lwc.system.service.MemberReadHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 会员浏览记录管理Service实现类
 */
@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {
    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;

    @Override
    public int create(MemberReadHistory memberReadHistory) {
        memberReadHistory.setId(null);// 如果有id的为修改，无id为添加
        memberReadHistory.setCreateTime(new Date());
        memberReadHistoryRepository.save(memberReadHistory);
        return 1;
    }

    @Override
    public int delete(List<String> ids) {
        List<MemberReadHistory> deleteList = new ArrayList<>();
        for(String id:ids){
            MemberReadHistory memberReadHistory = new MemberReadHistory();
            memberReadHistory.setId(id);
            deleteList.add(memberReadHistory);
        }
        memberReadHistoryRepository.deleteAll(deleteList);
        return ids.size();
    }

    @Override
    public List<MemberReadHistory> list(Long memberId) {
        return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(memberId);
    }


    /**
     * @Description: 根据Id查询
     * @Author: lwc
     * @Date: 2022-04-22 9:50
     */
    void findById() {
        // Optional为Java8新特性用于防治空指针异常
        Optional<MemberReadHistory> userOptional = memberReadHistoryRepository.findById("604dbbd87010e778b026ebe2");
        System.out.println(userOptional.get());
    }

    /**
     * @Description: 条件查询
     * @Author: lwc
     * @Date: 2022-04-22 9:47
     */

    void find() {
        MemberReadHistory memberReadHistory = new MemberReadHistory();

        memberReadHistory.setProductName("");
        // 全and
        Example<MemberReadHistory> example = Example.of(memberReadHistory);
        List<MemberReadHistory> userList = memberReadHistoryRepository.findAll(example);
        System.out.println(userList);
    }


    /**
     * @Description: 模糊查询
     * @Author: lwc
     * @Date: 2022-04-22 9:49
     */
    void findLike() {
        // ExampleMatcher.StringMatcher.CONTAINING 模糊匹配
        // withIgnoreCase 忽略大小写
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        MemberReadHistory memberReadHistory = new MemberReadHistory();

        memberReadHistory.setProductName("");
        Example<MemberReadHistory> example = Example.of(memberReadHistory,matcher);

        List<MemberReadHistory> userList = memberReadHistoryRepository.findAll(example);

        System.out.println(userList);

    }


    /**
     * @Description: 分页查询
     * @Author: lwc
     * @Date: 2022-04-22 9:50
     */
    void findPage() {
        // 定义排序规则
        Sort sort = Sort.by(Sort.Direction.DESC,"age");
        // 分页规则(第几页（0开始）， 多少个，排序规则)
        PageRequest page = PageRequest.of(0, 3, sort);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        MemberReadHistory memberReadHistory = new MemberReadHistory();
        memberReadHistory.setProductName("");
        Example<MemberReadHistory> example = Example.of(memberReadHistory , matcher);
        Page<MemberReadHistory> userPage = memberReadHistoryRepository.findAll(example, page);

        System.out.println(userPage.getTotalElements());
        System.out.println(userPage.getContent());
    }

}
